<!DOCTYPE html><html lang="zh-Hans" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>正则表达式 | DreamcatcherDENG 的个人博客</title><meta name="author" content="dreamcatcherdeng"><meta name="copyright" content="dreamcatcherdeng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="referrer" content="no-referrer"><meta name="description" content="本文介绍了正则表达式的常见知识点，并列举了正则表达式的方法。本文的知识点来自韩顺平的正则表达式讲解。">
<meta property="og:type" content="article">
<meta property="og:title" content="正则表达式">
<meta property="og:url" content="https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/RegularExpression/index.html">
<meta property="og:site_name" content="DreamcatcherDENG 的个人博客">
<meta property="og:description" content="本文介绍了正则表达式的常见知识点，并列举了正则表达式的方法。本文的知识点来自韩顺平的正则表达式讲解。">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/romance-2258599_960_720.jpg">
<meta property="article:published_time" content="2022-07-13T09:38:01.002Z">
<meta property="article:modified_time" content="2022-07-16T16:23:36.264Z">
<meta property="article:author" content="dreamcatcherdeng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/romance-2258599_960_720.jpg"><link rel="shortcut icon" href="/personal-blog/img/favicon.png"><link rel="canonical" href="https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/RegularExpression/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/personal-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/personal-blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '正则表达式',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-07-17 00:23:36'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/personal-blog/atom.xml" title="DreamcatcherDENG 的个人博客" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://n.sinaimg.cn/sinakd10100/452/w1080h972/20200416/93dc-iskepxs3894040.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/personal-blog/archives/"><div class="headline">Articles</div><div class="length-num">34</div></a></div><div class="data-item"><a href="/personal-blog/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/personal-blog/categories/"><div class="headline">Categories</div><div class="length-num">35</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/personal-blog/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/romance-2258599_960_720.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/personal-blog/">DreamcatcherDENG 的个人博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/personal-blog/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">正则表达式</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-07-13T09:38:01.002Z" title="Created 2022-07-13 17:38:01">2022-07-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-07-16T16:23:36.264Z" title="Updated 2022-07-17 00:23:36">2022-07-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/personal-blog/categories/Java/">Java</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/personal-blog/categories/Java/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">正则表达式</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">2.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>8min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="正则表达式"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>本文介绍了正则表达式的常见知识点，并列举了正则表达式的方法。本文的知识点来自韩顺平的正则表达式讲解。</p>
</blockquote>
<span id="more"></span>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Eq4y1E79W?from=search&amp;seid=3635718524423831028&amp;spm_id_from=333.337.0.0">视频地址</a></p>
<h1 id="正则表达式的概念"><a href="#正则表达式的概念" class="headerlink" title="正则表达式的概念"></a>正则表达式的概念</h1><p>使用单一的字符串模式描述或匹配一些列具有相同规则的字符串的一种公式。</p>
<h1 id="正则表达式的使用"><a href="#正则表达式的使用" class="headerlink" title="正则表达式的使用"></a>正则表达式的使用</h1><h2 id="转义符"><a href="#转义符" class="headerlink" title="转义符"></a>转义符</h2><blockquote>
<p>注：在java的正则表达式中，转义符是<code>\\</code>,即两个<code>\</code>相当于其他语言的一个<code>\</code></p>
</blockquote>
<ul>
<li>匹配一个转义符</li>
</ul>
<p><strong>原字符串使用两个反斜杠表示一个反斜杠，模式串使用四个反斜杠匹配一个反斜杠</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">str = <span class="string">&quot;hello\\world&quot;</span>；</span><br><span class="line">pattern = <span class="string">&quot;\\\\&quot;</span>;<span class="comment">//前两个\表示一个\，即正常的转义符，后两个\表示一个\，后面的\被前面的\转义使其不再具有转义的功能，从而正常表达为\，所以需要四个\表示一个\</span></span><br></pre></td></tr></table></figure>
<p><img src="http://124.221.123.212:8080/images/image-20220210112002027.png" alt="image-20220210112002027"></p>
<ul>
<li><p>匹配两个转义符</p>
<p><strong>原字符串使用四个反斜杠表示两个反斜杠，模式字符串使用八个反斜杠表示两个反斜杠</strong></p>
<p>通常需要被转义的字符有：</p>
<p><code>.*+()$/\?[]^&#123;&#125;</code></p>
</li>
</ul>
<h2 id="字符匹配符"><a href="#字符匹配符" class="headerlink" title="字符匹配符"></a>字符匹配符</h2><p>字符匹配符即元字符，用于匹配一般字符的字符</p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">符号</th>
<th style="text-align:center">符号解释</th>
<th style="text-align:center">示例</th>
<th style="text-align:center">示例解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>[]</code></td>
<td style="text-align:center">匹配中括号内的任意一个字符</td>
<td style="text-align:center"><code>[abcde]</code></td>
<td style="text-align:center">可以匹配a,b,c,d,e中的任意一个字符</td>
</tr>
<tr>
<td style="text-align:center"><code>[^]</code></td>
<td style="text-align:center">匹配除了<code>^</code>之后的任意一个字符</td>
<td style="text-align:center"><code>[^abcde]</code></td>
<td style="text-align:center">可以匹配除了a,b,c,d,e中的任意字符</td>
</tr>
<tr>
<td style="text-align:center"><code>-</code></td>
<td style="text-align:center">匹配连字符两端之间的任意一个字符</td>
<td style="text-align:center"><code>[a-e]</code></td>
<td style="text-align:center">可以匹配从a到z之间的任意一个字符(包括a和e),相当于<code>[abcde]</code></td>
</tr>
<tr>
<td style="text-align:center"><code>.</code></td>
<td style="text-align:center">匹配除了<code>\n</code>以外的任意一个字符</td>
<td style="text-align:center"><code>a...e</code></td>
<td style="text-align:center">可以匹配<code>adase</code>,<code>adsee</code>等，一个<code>.</code>表示任意一个除了<code>\n</code>之外的字符</td>
</tr>
<tr>
<td style="text-align:center"><code>\\d</code></td>
<td style="text-align:center">匹配单个数字字符</td>
<td style="text-align:center"><code>\\d</code></td>
<td style="text-align:center">相当于<code>[0-9]</code></td>
</tr>
<tr>
<td style="text-align:center"><code>\\D</code></td>
<td style="text-align:center">匹配单个非数字字符</td>
<td style="text-align:center"><code>\\D</code></td>
<td style="text-align:center">相当于<code>[^0-9]</code></td>
</tr>
<tr>
<td style="text-align:center"><code>\\w</code></td>
<td style="text-align:center">匹配单个数字、大小写字母、下划线</td>
<td style="text-align:center"><code>\\w</code></td>
<td style="text-align:center">相当于<code>[0-9a-zA-Z_]</code></td>
</tr>
<tr>
<td style="text-align:center"><code>\\W</code></td>
<td style="text-align:center">匹配单个非数字、大小写字母、下划线</td>
<td style="text-align:center"><code>\\W</code></td>
<td style="text-align:center">相当于<code>[^0-9a-zA-Z_]</code></td>
</tr>
<tr>
<td style="text-align:center"><code>(?i)</code></td>
<td style="text-align:center">不区分大小写匹配后面的字母</td>
<td style="text-align:center"><code>c(?i)abc</code></td>
<td style="text-align:center">可以匹配<code>cabc,cABC</code></td>
</tr>
<tr>
<td style="text-align:center"><code>\\s</code></td>
<td style="text-align:center">匹配任何空白字符</td>
<td style="text-align:center"><code>\\s</code></td>
<td style="text-align:center">可以匹配空格，制表符等</td>
</tr>
<tr>
<td style="text-align:center"><code>\\S</code></td>
<td style="text-align:center">匹配任何非空白字符</td>
<td style="text-align:center"><code>\\S</code></td>
<td style="text-align:center">匹配任何可以显示的字符</td>
</tr>
</tbody>
</table>
</div>
<blockquote>
<p>注：若要使匹配字符串时大小写不敏感，还可以使用<code>Pattern.compile(reStr,Pattern.CASE_INSENSITIVE)</code>进行大小写不敏感匹配</p>
<p><code>[]</code>内的元字符表示字符本身的意思，不再具有特殊意义</p>
</blockquote>
<h2 id="选择匹配符"><a href="#选择匹配符" class="headerlink" title="选择匹配符"></a>选择匹配符</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">符号</th>
<th style="text-align:center">符号解释</th>
<th style="text-align:center">示例</th>
<th style="text-align:center">示例解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">`</td>
<td style="text-align:center">`</td>
<td style="text-align:center">选择匹配符，匹配符号左右两端任意一个模式串</td>
<td style="text-align:center">`abc</td>
<td>DEF`</td>
<td>可以匹配<code>abc</code>,或<code>DEF</code></td>
</tr>
</tbody>
</table>
</div>
<p><img src="http://124.221.123.212:8080/images/image-20220302182444082.png" alt="image-20220302182444082"></p>
<h2 id="限定符"><a href="#限定符" class="headerlink" title="限定符"></a>限定符</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">符号</th>
<th style="text-align:center">符号解释</th>
<th style="text-align:center">示例</th>
<th style="text-align:center">示例解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>*</code></td>
<td style="text-align:center">指定字符重复0次到n次</td>
<td style="text-align:center"><code>(abc)*</code></td>
<td style="text-align:center">可以匹配空白字符,<code>abc，abcabc,abcabcabc</code>等</td>
</tr>
<tr>
<td style="text-align:center"><code>+</code></td>
<td style="text-align:center">指定字符重复1次到n次</td>
<td style="text-align:center"><code>m+(abc)*</code></td>
<td style="text-align:center">至少以一个m开头，0个或多个<code>abc</code>，可以匹配<code>mabc,mabcabc,mmmabc等</code></td>
</tr>
<tr>
<td style="text-align:center"><code>?</code></td>
<td style="text-align:center">指定字符重复0次到1次</td>
<td style="text-align:center"><code>m?(abc)*</code></td>
<td style="text-align:center">以0个或1个m开头，0个或多个<code>abc</code>，可以匹配<code>abc,mabc,mabcabc,mmmabc等</code></td>
</tr>
<tr>
<td style="text-align:center"><code>&#123;n&#125;</code></td>
<td style="text-align:center">指定n个前面字符</td>
<td style="text-align:center">`a{3}</td>
<td style="text-align:center">[abc]{3}`</td>
<td>可以匹配<code>aaa,abc，abb,bbb,ccc,abc,cba等</code></td>
</tr>
<tr>
<td style="text-align:center"><code>&#123;n,m&#125;</code></td>
<td style="text-align:center">指定n到m个字符</td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"><code>&#123;n,&#125;</code></td>
<td style="text-align:center">指定至少n个字符</td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">{,m}</td>
<td style="text-align:center">指定最多m个字符</td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
</div>
<h3 id="贪婪匹配"><a href="#贪婪匹配" class="headerlink" title="贪婪匹配"></a>贪婪匹配</h3><ul>
<li>java的匹配规则是<strong>贪婪匹配</strong></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">str = <span class="string">&quot;11111&quot;</span>;</span><br><span class="line">pattern = <span class="string">&quot;1&#123;3,4&#125;&quot;</span>;</span><br></pre></td></tr></table></figure>
<p>则按照贪婪匹配原则，pattern将匹配 <strong>4个1而不是3个1</strong></p>
<p><img src="http://124.221.123.212:8080/images/image-20220210104620666.png" alt="image-20220210104620666"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">str=<span class="string">&quot;1111111&quot;</span>;</span><br><span class="line">pattern = <span class="string">&quot;&#123;3,4&#125;&quot;</span>;</span><br></pre></td></tr></table></figure>
<p>则按照贪婪匹配原则,pattern将先匹配 <strong>4个1,再匹配3个1</strong></p>
<p><img src="http://124.221.123.212:8080/images/image-20220210104713981.png" alt="image-20220210104713981"></p>
<h3 id="非贪婪匹配"><a href="#非贪婪匹配" class="headerlink" title="非贪婪匹配"></a>非贪婪匹配</h3><p>若想使用非贪婪匹配，只需在限定符后面加<code>?</code></p>
<ul>
<li><img src="http://124.221.123.212:8080/images/image-20220302201400708.png" alt="image-20220302201400708"></li>
</ul>
<h2 id="定位符"><a href="#定位符" class="headerlink" title="定位符"></a>定位符</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">符号</th>
<th style="text-align:center">符号解释</th>
<th style="text-align:center">示例</th>
<th style="text-align:center">示例解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>^</code></td>
<td style="text-align:center">指定起始字符</td>
<td style="text-align:center"><code>^[0-9]+</code></td>
<td style="text-align:center">匹配以0-9数字开头的字符如<code>0a,1abc,111</code></td>
</tr>
<tr>
<td style="text-align:center">$$`</td>
<td style="text-align:center">指定结束字符</td>
<td style="text-align:center">`[a-z]$$</td>
<td style="text-align:center">匹配以a-z结尾的字符<code>023a,23b</code></td>
</tr>
<tr>
<td style="text-align:center"><code>\\b</code></td>
<td style="text-align:center">指定目标串的边界</td>
<td style="text-align:center"><code>abc\\b</code></td>
<td style="text-align:center">可以匹配字符串<code>abcddd dddabc abcddd</code>中的<code>dddabc</code>的<code>abc</code></td>
</tr>
<tr>
<td style="text-align:center"><code>\\B</code></td>
<td style="text-align:center">指定目标串的非边界</td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
</div>
<h2 id="捕获分组"><a href="#捕获分组" class="headerlink" title="捕获分组"></a>捕获分组</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">捕获形式</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>(pattern)</code></td>
<td style="text-align:center">非命名捕获，捕获内容使用捕获的组号来获取</td>
</tr>
<tr>
<td style="text-align:center"><code>(?&lt;name&gt;pattern)</code></td>
</tr>
</tbody>
</table>
</div>
<p><img src="http://124.221.123.212:8080/images/image-20220302204724900.png" alt="image-20220302204724900"></p>
<p><img src="http://124.221.123.212:8080/images/image-20220302205421421.png" alt="image-20220302205421421"></p>
<h2 id="非捕获分组"><a href="#非捕获分组" class="headerlink" title="非捕获分组"></a>非捕获分组</h2><p><img src="http://124.221.123.212:8080/images/image-20220302205619129.png" alt="image-20220302205619129"> </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp08</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String content = <span class="string">&quot;在线学习 在线教育 在线玩游戏&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//只匹配“在线学习”和“在线教育”</span></span><br><span class="line">        String regStr = <span class="string">&quot;在线(?:学习|教育)&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        //只匹配“在线学习”和“在线教育”的“在线”</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;在线(?=学习|教育)&quot;;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //只匹配“在线玩游戏”的在线</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;在线(?!学习|教育)&quot;;</span></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(content);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())&#123;</span><br><span class="line">            System.out.println(matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="反向引用"><a href="#反向引用" class="headerlink" title="反向引用"></a>反向引用</h2><p>圆括号内的内容被捕获后可以在圆括号后被使用，从而写出与圆括号内容相同的内容，这个方法被称为反向引用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp13</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//获取五位数的回文串</span></span><br><span class="line">        String regStr = <span class="string">&quot;(\\d)(\\d)(\\d)\\2\\1&quot;</span>;</span><br><span class="line">        String content = <span class="string">&quot;123451232145654&quot;</span>;</span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(content);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())&#123;</span><br><span class="line">            System.out.println(matcher.group());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">12321</span></span><br><span class="line"><span class="comment">45654</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp14</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String content = <span class="string">&quot;我...我要...学学学...java&quot;</span>;</span><br><span class="line">        String s = <span class="string">&quot;\\.&quot;</span>;</span><br><span class="line">        String s1 = <span class="string">&quot;(.)\\1+&quot;</span>;</span><br><span class="line">        System.out.println(content);</span><br><span class="line">        Pattern pattern = Pattern.compile(s);</span><br><span class="line">        Matcher matcher = pattern.matcher(content);</span><br><span class="line">        <span class="comment">//将.号删除</span></span><br><span class="line">        content = matcher.replaceAll(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        System.out.println(content);</span><br><span class="line">        <span class="comment">//将连续的重复字替换为一个</span></span><br><span class="line">        pattern = Pattern.compile(s1);</span><br><span class="line">        matcher = pattern.matcher(content);</span><br><span class="line">        content = matcher.replaceAll(<span class="string">&quot;$1&quot;</span>);</span><br><span class="line">        System.out.println(content);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">我...我要...学学学...java</span></span><br><span class="line"><span class="comment">我我要学学学java</span></span><br><span class="line"><span class="comment">我要学java</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注：<code>\\分组好</code>为反向引用，在<code>Pattern.compile()</code>函数编译时使用，<code>$分组号</code>也为反向引用，在编译之后使用</p>
</blockquote>
<h2 id="正则表达式的分组原理"><a href="#正则表达式的分组原理" class="headerlink" title="正则表达式的分组原理"></a>正则表达式的分组原理</h2><h3 id="获取分组"><a href="#获取分组" class="headerlink" title="获取分组"></a>获取分组</h3><p>模式串中，每一个<code>()</code>代表一个分组，分组从<code>1</code>开始，使用<code>matcher.group(0)</code>获取模式串匹配到的整个文本，使用<code>matcher.group(1)</code>获得第一个分组，使用<code>matcher.group(1)</code>获得第二个分组，依此类推。</p>
<h3 id="matcher-group-源码"><a href="#matcher-group-源码" class="headerlink" title="matcher.group()源码"></a><code>matcher.group()</code>源码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">group</span><span class="params">(<span class="keyword">int</span> group)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (first &lt; <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">&quot;No match found&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span> (group &lt; <span class="number">0</span> || group &gt; groupCount())</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IndexOutOfBoundsException(<span class="string">&quot;No group &quot;</span> + group);</span><br><span class="line">        <span class="keyword">if</span> ((groups[group*<span class="number">2</span>] == -<span class="number">1</span>) || (groups[group*<span class="number">2</span>+<span class="number">1</span>] == -<span class="number">1</span>))</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">return</span> getSubSequence(groups[group * <span class="number">2</span>], groups[group * <span class="number">2</span> + <span class="number">1</span>]).toString();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>传入的参数<code>group</code>表示分组号，<code>groups[]</code>数组中存放着匹配到的分组的索引信息，<code>groups[0]到groups[1]</code>存放的是整个模式串匹配到的字符串的第一个字符到最后一个字符的索引，<code>groups[group*2]到groups[group*2+1)]</code>存放的是第<code>group</code>个分组的第一个和最后一个字符的索引。</p>
<h1 id="正则表达式常用类及方法"><a href="#正则表达式常用类及方法" class="headerlink" title="正则表达式常用类及方法"></a>正则表达式常用类及方法</h1><h2 id="Pattern类"><a href="#Pattern类" class="headerlink" title="Pattern类"></a><code>Pattern</code>类</h2><h3 id="Pattern-matches（regex-content）"><a href="#Pattern-matches（regex-content）" class="headerlink" title="Pattern.matches（regex,content）"></a><code>Pattern.matches（regex,content）</code></h3><p>若模式串<code>regex</code>字符串<code>content</code>能够整体匹配，返回结果为<code>true</code>否则为<code>false</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp11</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String content = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">        String regex1 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">		String regex2 = <span class="string">&quot;hello.*&quot;</span>;</span><br><span class="line">        System.out.println(Pattern.matches(regex1, content));<span class="comment">//false</span></span><br><span class="line">        System.out.println(Pattern.matches(regex2, content));<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="Matcher类"><a href="#Matcher类" class="headerlink" title="Matcher类"></a><code>Matcher</code>类</h2><h3 id="matcher-start-和matcher-end"><a href="#matcher-start-和matcher-end" class="headerlink" title="matcher.start()和matcher.end()"></a><code>matcher.start()和matcher.end()</code></h3><p>返回每次匹配到的字符开始和结束的索引</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp12</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String content = <span class="string">&quot;hello world hello&quot;</span>;</span><br><span class="line">        String regex1 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regex1);</span><br><span class="line">        Matcher matcher = pattern.matcher(content);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())&#123;</span><br><span class="line">            System.out.println(matcher.start());</span><br><span class="line">            System.out.println(matcher.end());</span><br><span class="line">            System.out.println(content.substring(matcher.start(),matcher.end()));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">0</span></span><br><span class="line"><span class="comment">5</span></span><br><span class="line"><span class="comment">hello</span></span><br><span class="line"><span class="comment">12</span></span><br><span class="line"><span class="comment">17</span></span><br><span class="line"><span class="comment">hello</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p><code>matcher.matches()</code></p>
<p>也表示整体匹配，和<code>Pattern.matches(regex,content)</code>一样返回结果为<code>boolean</code>类型</p>
<p><code>matcher.replaceAll(replaceStr)</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp12</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String content = <span class="string">&quot;hello world hello&quot;</span>;</span><br><span class="line">        String regex1 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regex1);</span><br><span class="line">        Matcher matcher = pattern.matcher(content);</span><br><span class="line">      	String world = matcher.replaceAll(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        System.out.println(world);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">world world world</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注：<code>matcher.replaceAll（replaceStr）</code>不替换原有的文本，而是拷贝后替换返回新的文本</p>
</blockquote>
<h1 id="String类中的正则表达式的使用"><a href="#String类中的正则表达式的使用" class="headerlink" title="String类中的正则表达式的使用"></a><code>String类</code>中的正则表达式的使用</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegExp15</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//relaceAll()方法</span></span><br><span class="line">        String content = <span class="string">&quot;hello world hi wolrd&quot;</span>;</span><br><span class="line">        content = content.replaceAll(<span class="string">&quot;hello|hi&quot;</span>,<span class="string">&quot;你好&quot;</span>);</span><br><span class="line">        System.out.println(content);</span><br><span class="line">	</span><br><span class="line">		<span class="comment">//matches()方法</span></span><br><span class="line">        String phone = <span class="string">&quot;13888888888&quot;</span>;</span><br><span class="line">        <span class="keyword">if</span> (phone.matches(<span class="string">&quot;^1[3|4|5|7|8][0-9]&#123;9&#125;$&quot;</span>))&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;是电话号码！&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;不是电话号码！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//split()方法</span></span><br><span class="line">        String splitStr = <span class="string">&quot;hello#world&amp;hi%wolrd&quot;</span>;</span><br><span class="line">        String[] split = splitStr.split(<span class="string">&quot;[#&amp;%]&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (String s : split)&#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">你好 world 你好 wolrd</span></span><br><span class="line"><span class="comment">是电话号码！</span></span><br><span class="line"><span class="comment">hello</span></span><br><span class="line"><span class="comment">world</span></span><br><span class="line"><span class="comment">hi</span></span><br><span class="line"><span class="comment">wolrd</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">dreamcatcherdeng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/RegularExpression/">https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/RegularExpression/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/romance-2258599_960_720.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/personal-blog/2022/07/13/Nodejs/"><img class="next-cover" src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-0qxx54.jpg" onerror="onerror=null;src='/personal-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">JavaScript and NodeJs</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://n.sinaimg.cn/sinakd10100/452/w1080h972/20200416/93dc-iskepxs3894040.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">dreamcatcherdeng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/personal-blog/archives/"><div class="headline">Articles</div><div class="length-num">34</div></a></div><div class="card-info-data-item"><a href="/personal-blog/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/personal-blog/categories/"><div class="headline">Categories</div><div class="length-num">35</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/DreamcatcherDENG" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">正则表达式的概念</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">正则表达式的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BD%AC%E4%B9%89%E7%AC%A6"><span class="toc-number">2.1.</span> <span class="toc-text">转义符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E5%8C%B9%E9%85%8D%E7%AC%A6"><span class="toc-number">2.2.</span> <span class="toc-text">字符匹配符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%8C%B9%E9%85%8D%E7%AC%A6"><span class="toc-number">2.3.</span> <span class="toc-text">选择匹配符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%99%90%E5%AE%9A%E7%AC%A6"><span class="toc-number">2.4.</span> <span class="toc-text">限定符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B4%AA%E5%A9%AA%E5%8C%B9%E9%85%8D"><span class="toc-number">2.4.1.</span> <span class="toc-text">贪婪匹配</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E8%B4%AA%E5%A9%AA%E5%8C%B9%E9%85%8D"><span class="toc-number">2.4.2.</span> <span class="toc-text">非贪婪匹配</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9A%E4%BD%8D%E7%AC%A6"><span class="toc-number">2.5.</span> <span class="toc-text">定位符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8D%95%E8%8E%B7%E5%88%86%E7%BB%84"><span class="toc-number">2.6.</span> <span class="toc-text">捕获分组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9D%9E%E6%8D%95%E8%8E%B7%E5%88%86%E7%BB%84"><span class="toc-number">2.7.</span> <span class="toc-text">非捕获分组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8D%E5%90%91%E5%BC%95%E7%94%A8"><span class="toc-number">2.8.</span> <span class="toc-text">反向引用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E5%88%86%E7%BB%84%E5%8E%9F%E7%90%86"><span class="toc-number">2.9.</span> <span class="toc-text">正则表达式的分组原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%88%86%E7%BB%84"><span class="toc-number">2.9.1.</span> <span class="toc-text">获取分组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#matcher-group-%E6%BA%90%E7%A0%81"><span class="toc-number">2.9.2.</span> <span class="toc-text">matcher.group()源码</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%B8%B8%E7%94%A8%E7%B1%BB%E5%8F%8A%E6%96%B9%E6%B3%95"><span class="toc-number">3.</span> <span class="toc-text">正则表达式常用类及方法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Pattern%E7%B1%BB"><span class="toc-number">3.1.</span> <span class="toc-text">Pattern类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Pattern-matches%EF%BC%88regex-content%EF%BC%89"><span class="toc-number">3.1.1.</span> <span class="toc-text">Pattern.matches（regex,content）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Matcher%E7%B1%BB"><span class="toc-number">3.2.</span> <span class="toc-text">Matcher类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#matcher-start-%E5%92%8Cmatcher-end"><span class="toc-number">3.2.1.</span> <span class="toc-text">matcher.start()和matcher.end()</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#String%E7%B1%BB%E4%B8%AD%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">4.</span> <span class="toc-text">String类中的正则表达式的使用</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/RegularExpression/" title="正则表达式"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/romance-2258599_960_720.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="正则表达式"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/RegularExpression/" title="正则表达式">正则表达式</a><time datetime="2022-07-13T09:38:01.002Z" title="Created 2022-07-13 17:38:01">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/Nodejs/" title="JavaScript and NodeJs"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-0qxx54.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="JavaScript and NodeJs"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/Nodejs/" title="JavaScript and NodeJs">JavaScript and NodeJs</a><time datetime="2022-07-13T09:38:00.960Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/MySQL/" title="MySQL"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-e7ek7k.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="MySQL"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/MySQL/" title="MySQL">MySQL</a><time datetime="2022-07-13T09:38:00.957Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/JAVA/" title="JAVA"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-72rd8e.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="JAVA"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/JAVA/" title="JAVA">JAVA</a><time datetime="2022-07-13T09:38:00.948Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/ViewPager/" title="ViewPager"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-0qxx54.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="ViewPager"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/ViewPager/" title="ViewPager">ViewPager</a><time datetime="2022-07-13T09:38:00.929Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By dreamcatcherdeng</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Toggle Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Local search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/personal-blog/js/utils.js"></script><script src="/personal-blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/personal-blog/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css"><script src="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/copy-tex.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/copy-tex.css"><script>(() => {
  document.querySelectorAll('#article-container span.katex-display').forEach(item => {
    btf.wrap(item, 'div', { class: 'katex-wrap'})
  })
})()</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/personal-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/personal-blog/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":300,"hOffset":0,"vOffset":0,"opacity":0.1},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>